package com.benben.springSecurity.util;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class UserLonginIntercepor extends HandlerInterceptorAdapter {

    Logger logger = Logger.getLogger(UserLonginIntercepor.class);
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.info("用户校验拦截");
        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();
        if(session.getAttribute("user")!=null){
            logger.info("用户校验成功");
            return true;
        }else{
            logger.info("用户校验时报");
            response.sendRedirect("/index");
            return false;
        }
    }

}
